home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre1.z / postgre1 / test / postfs / loadfile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-27  |  888 b   |  45 lines

  1. /* usage: loadfile unix-path postfs-path bufsize(in bytes) */
  2. #include <sys/file.h>
  3. #include "tmp/libpq-fs.h"
  4. #include "catalog/pg_lobj.h"
  5.  
  6. extern char *PQexec();
  7.  
  8. void main(ac,av)
  9.      int ac;
  10.      char *av[];
  11. {
  12.     int fd, pfd;
  13.     int n, pn;
  14.     int blen = 65536;
  15.     char *buf;
  16.     char *res;
  17.  
  18.     PQsetdb(getenv("USER"));
  19.     if (ac != 3) {
  20.     printf ("usage: %s unix-path postfs-path\n",av[0]);
  21.     exit(1);
  22.     }
  23.  
  24.     buf = (char *)malloc(blen);
  25.  
  26.     res = PQexec("begin");
  27.     if (p_open(av[2],O_RDONLY) < 0) {
  28.     fd = open(av[1],O_RDONLY);
  29.     pfd = p_creat(av[2],0666,Unix);
  30.     while ((n = read(fd,buf,blen)) > 0) {
  31.         if ((pn = p_write(pfd,buf,n)) != n) {
  32.         printf("expected %d, got %d\n", n, pn);
  33.         }
  34.         printf ("p_write returned %d\n",pn);
  35.     }
  36.     close(fd);
  37.     p_close(pfd);
  38.     } else {
  39.     printf ("file already exists\n");
  40.     }
  41.     res = PQexec("end");
  42.     PQfinish();
  43.     exit(0);
  44. }
  45.